{{define "chats/index"}}
	<script>
		WEB_SOCKET_SWF_LOCATION = "/public/WebSocketMain.swf";
	  $(function() {
	  	var conn;
	    var msg = $("#msg");
	    var log = $("#log");

	    function appendLog(msg) {
        var d = log[0]
        var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
        msg.appendTo(log)
        if (doScroll) {
            d.scrollTop = d.scrollHeight - d.clientHeight;
        }
	    }

			$("#form").submit(function() {
        if (!conn) {
            return false;
        }
        if (!msg.val()) {
            return false;
        }
        console.log(msg.val());
        conn.send(msg.val());
        msg.val("");
        return false
	    });

	    if (window["WebSocket"]) {
	        conn = new WebSocket("ws://{{.WebSocketHost}}/chat");
	        conn.onclose = function(evt) {
	            appendLog($("<div><b>Connection closed.</b></div>"))
	        }
	        conn.onmessage = function(evt) {
	            appendLog($("<div/>").text(evt.data))
	        }
	    } else {
	        appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
	    }
		});
	</script>

	<div id="log"></div>
	<form id="form">
	    <input type="submit" value="Send" />
	    <input type="text" id="msg" size="64"/>
	</form>
{{end}}
